<?php 
    class Tk_accounts_subject_model extends CI_Model{

        function __construct(){
            parent::__construct();
        }

        function get_top_subject_list(){
            $sql = "SELECT id,pid,title FROM {$this->db->dbprefix}tk_accounts_subject where status = 1 AND pid = 0";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_subject_list_by_pid($pid){
            $sql = "SELECT s.id,s.pid,s.title, count(s1.id) AS children FROM {$this->db->dbprefix}tk_accounts_subject AS s ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}tk_accounts_subject AS s1 ON(s1.pid=s.id) ";
            $sql .= " where s.status = 1 AND s.pid = $pid GROUP BY s.id";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }
    }